package com.example.flashsales.controller;

import com.example.flashsales.Factory;
import com.example.flashsales.Tools.BeanUtils;
import com.example.flashsales.entity.Result;
import com.example.flashsales.entity.pojo.User;
import com.example.flashsales.entity.vo.UserVo;
import com.example.flashsales.role.role;
import com.example.flashsales.service.impl.UserStateServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/adminUser")
public class AdminUserController {

//    private final Factory factory = BeanUtils.getBean(Factory.class);

    Factory factory = Factory.getFactory();

//    @Autowired
//    Factory factory;

    @Autowired
    UserStateServiceImpl userStateService;

    /**
     * 查看在线用户
     * @param httpServletRequest
     * @return
     */

    @ResponseBody
    @role(role = 33)
    @GetMapping("/viewOnlineUser")
    public Result viewOnlineUser(HttpServletRequest httpServletRequest,@RequestParam(defaultValue = "0") Long userId){
        Map<String,Object> map = new HashMap<>();
        System.out.println(userId);
        if (userId.equals(0) || userId == 0){
            final Map<Long, UserVo> onlineUsers = factory.getOnlineUsers();
            List<UserVo> list = new ArrayList<>();
            for (Long aLong : onlineUsers.keySet()) {
                list.add(onlineUsers.get(aLong));
            }
            map.put("total",onlineUsers.size());
            map.put("data",list);
            return Result.ok("查看成功").setData(map);
        }
        final UserVo userVo = factory.getOnlineUsers().get(userId);
        if (userVo == null){
            return Result.error("用户不在线");
        }else {
            map.put("total",1);
            map.put("data",userVo);
            return Result.ok("查看成功").setData(map);
        }
    }

    /**
     * 下线用户
     * @param httpServletRequest
     * @param userId 用户id
     * @return
     */
    @ResponseBody
    @role(role = 32)
    @GetMapping("/offlineUser")
    public Result offlineUser(HttpServletRequest httpServletRequest,Long userId){
        if (userId == null){
            return Result.error("请输入用户id");
        }
        if (!factory.getOnlineUsers().containsKey(userId)) {
            return Result.error("用户不在线");
        }
        factory.getOnlineUsers().remove(userId);
        return Result.ok("下线成功");
    }


    /**
     * 冻结用户
     * @param httpServletRequest
     * @param userId 用户id
     * @return
     */
    @ResponseBody
    @role(role = 34)
    @GetMapping("/freezeUser")
    public Result freezeUser(HttpServletRequest httpServletRequest,Long userId){
        if (userId == null){
            return Result.error("请输入用户id");
        }
        final User user = userStateService.viewUser(userId);
        if (user == null){
            return Result.error("用户不存在");
        }
        if (user.getState() == 1){
            return Result.error("用户已经被封号了");
        }
        //修改用户状态
        if (userStateService.updateUserState(userId,1) > 0) {
            //判断用户是否在线
            if (factory.getOnlineUsers().containsKey(userId)) {
                //如果在线 下线用户
                factory.getOnlineUsers().remove(userId);
            }
            return Result.ok("冻结成功");
        }
        return Result.error("冻结失败");
    }


    /**
     * 解冻用户
     * @param httpServletRequest
     * @param userId 用户id
     * @return
     */
    @ResponseBody
    @role(role = 35)
    @GetMapping("/thawUser")
    public Result thawUser(HttpServletRequest httpServletRequest,Long userId){
        if (userId == null){
            return Result.error("请输入用户id");
        }
        final User user = userStateService.viewUser(userId);
        if (user == null){
            return Result.error("用户不存在");
        }
        if (user.getState() == 0){
            return Result.error("用户已经被解封了");
        }
        if (userStateService.updateUserState(userId,0) > 0) {
            return Result.ok("解冻成功");
        }
        return Result.error("解冻失败");
    }
}
